﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;
using CinePlancito.Dominio;

namespace CinePlancito.Controllers
{
    public class ComboController : Controller
    {

        string RestCombos = "http://localhost:15497/Combos.svc/Combos";
        JavaScriptSerializer js = new JavaScriptSerializer();

        // GET: /Alumno/Details/5
        public ActionResult DetallesCombo(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestCombos + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string comboJson = reader.ReadToEnd();
            Combo combo = js.Deserialize<Combo>(comboJson);
            return View(combo);
        }
        
        public ActionResult ListaCombos()
        {
            HttpWebRequest req5 = (HttpWebRequest)WebRequest
                .Create(RestCombos + "/");
            req5.Method = "GET";

            HttpWebResponse res5 = (HttpWebResponse)req5.GetResponse();
            StreamReader reader5 = new StreamReader(res5.GetResponseStream());
            string comboJson5 = reader5.ReadToEnd();
            List<Combo> combos = js.Deserialize<List<Combo>>(comboJson5);
            return View(combos);
        }
        public ActionResult RegistrarCombo()
        {
            return View();
        }
        //CREAR COMBO CON REST
        [HttpPost]
        public ActionResult RegistrarCombo(FormCollection collection)
        {
            try
            {

                int idcombo = int.Parse(collection["IdCombo"]);
                string nombre = (string)collection["Nombre"];
                string descripcion = (string)collection["Descripcion"];

                const string quote = "\"";
                string postdata = "{"
                                + quote + "IdCombo" + quote + ":" + quote + idcombo + quote + ","
                                + quote + "Nombre" + quote + ":" + quote + nombre + quote + ","
                                + quote + "Descripcion" + quote + ":" + quote + descripcion + quote +
                                  "}";

                // CREAR REGISTRO 
                //string postdata = "{\"IdCombo\":\"4\",\"Nombre\":\"Combo 4\",\"Descripcion\":\"GASEOSA MEDIANA\"}"; //jason
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest
                    .Create(RestCombos);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                return RedirectToAction("ListaCombos");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult EditarCombo(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestCombos + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string comboJson = reader.ReadToEnd();
            Combo combo = js.Deserialize<Combo>(comboJson);
            return View(combo);
        }

        // POST: /Alumno/Edit/5
        [HttpPost]
        public ActionResult EditarCombo(string id, FormCollection collection)
        {
            try
            {
                string nombre = (string)collection["Nombre"];
                string descripcion = (string)collection["Descripcion"];

                const string quote = "\"";
                string postdata = "{"
                                + quote + "IdCombo" + quote + ":" + quote + id + quote + ","
                                + quote + "Nombre" + quote + ":" + quote + nombre + quote + ","
                                + quote + "Descripcion" + quote + ":" + quote + descripcion + quote +
                                  "}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestCombos);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("ListaCombos");
            }
            catch
            {
                return View();
            }
        }



        public ActionResult EliminarCombo(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestCombos + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string comboJson = reader.ReadToEnd();
            Combo combo = js.Deserialize<Combo>(comboJson);
            return View(combo);
        }
        [HttpPost]
        public ActionResult EliminarCombo(string id, FormCollection collection)
        {
            try
            {

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestCombos + "/" + id);
                req.Method = "DELETE";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string comboJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Combo comboEliminado = js.Deserialize<Combo>(comboJson);
                return RedirectToAction("ListaCombos");
            }
            catch
            {
                return View();
            }
        }

    }
}